package org.map.core
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.system.ApplicationDomain;
	
	import gs.TweenLite;
	import gs.easing.Cubic;
	
	import org.core.BitmapMovieClip;
	import org.data.enum.ItemType;
	import org.data.model.LinkageModel;
	import org.interfaces.ILoader;
	import org.manager.GlobalManager;
	import org.manager.LoaderManager;
	import org.manager.SourceIDManager;
	import org.manager.TimerManager;
	import org.manager.TipManager;
	import org.net.AbcLoaderBinary;
	import org.net.AbcLoaderQueue;
	import org.utils.ClassUtil;
	import org.utils.InstanceUtil;

	public class IsoLoader extends Sprite
	{
		
		/**
		 * 对象数值 
		 */	    
		protected var _vo:IsoVO=new IsoVO()
		
		/**
		 * 素材程序域
		 */		
		protected var domain:ApplicationDomain
		/**
		 * 是否优化成位图缓存
		 */		
		protected var isCacheBitmap:Boolean=true
			
		/**
		 * 优化对象层级
		 */			
	    protected var level:int=2
			
		/**
		 * 素材对象 
		 */			
		protected var child:BitmapMovieClip
		protected var childNoCache:MovieClip
		
		/*protected function get childNoCache():MovieClip{return _childNoCache}
		protected function set childNoCache(value:MovieClip):void{
		
			_childNoCache=value
				
			if(value==null){
			
				trace(value)
			}
		}*/

		public function IsoLoader()
		{
			addEventListener(Event.ADDED_TO_STAGE,ADDED_TO_STAGE)
			addEventListener(Event.REMOVED_FROM_STAGE,REMOVED_FROM_STAGE)
		}
		
		/**
		 * 加载到显示列表
		 * @param e
		 * 
		 */		
		
		protected function ADDED_TO_STAGE(e:Event):void{
			
			
		}
		/**
		 * 从显示列表删除 
		 * @param e
		 * 
		 */		
		protected function REMOVED_FROM_STAGE(e:Event):void{
			
			removeEventListener(Event.REMOVED_FROM_STAGE,REMOVED_FROM_STAGE)
			
			if(child)TipManager.instance.removeAllTips(child.parent as Sprite)
			
		}
		
		/**
		 * 停止到某 帧状态
		 * @param frame
		 * 
		 */		
		public function gotoAndStop(frame:int):void{
			
			if(child){
				
				child.gotoFrame(frame)
				
			}
			
		}
		
		/**
		 * 初始化视图  
		 * @param cacheBitmap:是否优化成位图缓存
		 * @param _domain:资源素材程序域
		 * @param _level:优化对象层级
		 */	
		protected var linkage:String
		public function initView(cacheBitmap:Boolean=true,_domain:ApplicationDomain=null,_level:int=2):void{
			
			level=_level
			
			isCacheBitmap=cacheBitmap
			
			domain=_domain||GlobalManager.instance.defaultDomain
				
			linkage=SourceIDManager.instance.getItemLinkageByID(String(_vo.id))
			
			addLoader(linkage,domain)
			
		}
			
		/**
		 * 设置旋转方向
		 * @param direct 方向 取DirectType属性
		 * @param cacheBitmap 
		 * 
		 */		
		public function setRotation(direct:int):void{
			
			_vo.rotation=direct
			
			var rep:int=_vo.cols
			
			_vo.cols=_vo.rows
			
			_vo.rows=rep
			
			initView(isCacheBitmap,domain)
			
		}
		
		/**
		 * 加载素材，如果已经预加载则不显示loading,如果没有预加载则显示loading同时加载相关素材swf
		 * @param linkage 素材linkage连接
		 * @param domain  素材程序域
		 * 
		 */	
		protected var loader:MovieClip=new MovieClip()
		protected function addLoader(linkage:String,domain:ApplicationDomain=null):void{				
			
			domain=domain||GlobalManager.instance.defaultDomain				

			//得到当前需要的素材swf地址
			var url:String=LinkageModel.getIns().getSwfUrlByLinkAndDomain(linkage,domain);			
			
			//得到所有该链接名的素材数
			var url_Domain_Arr:Array=LinkageModel.getIns().getAllSwfUrl(linkage)
			
			if(LoaderManager.instance.getLoader(url)==null){
				
				//loader=ClassUtil.getInstanceByLinkage("loaderMc")
				
				this.addChild(loader)
				
				//trace("url:"+url,"linkage:"+linkage)
				if(url!=""){	
					
					for(var i:int=0;i<url_Domain_Arr.length;i++){		
						
						LoaderManager.instance.registerLoadQueueHandler(AbcLoaderBinary,url_Domain_Arr[i][0],addLoaderBySWF,url_Domain_Arr[i][1],"",10)						  
					}
					
					LoaderManager.instance.onLoadQueue(10)
						
				}else{						
					
					addLoaderByNoSWF(linkage)
				}
				
			}else{
				
				addLoaderByNoSWF(linkage)
				
			}
			
			
		}	
		
		
		/**
		 * 响应addLoader方法即时加载素材成功后调用
		 * 
		 */		
		protected function addLoaderByNoSWF(linkage:String):void{
			
			if(loader){
				
			if(this.contains(loader))this.removeChild(loader)	
				
			loader.stop()
				
			}
			
			isLoaded=true
				
			//如果已经存在则停止播放动画
			if(child){
				removeChild(child)
				child.dispose()
				child=null
				InstanceUtil.cleanObjByType(DisplayObject,this)
			}
			
			if(childNoCache){
				if(this.contains(childNoCache))removeChild(childNoCache)
				childNoCache=null
				InstanceUtil.cleanObjByType(DisplayObject,this)
			}
			
			//获取素材
			var mc:MovieClip=ClassUtil.getInstanceByLinkage(linkage,domain)
			
			mc.gotoAndStop(_vo.rotation)
			
			//优化
			cacheBitmap(mc,level)
			
		}
		/**
		 *
		 * @param level: 对mc分两层优化，默认层级为2
		 * 
		 */		
		
		protected function cacheBitmap(mc:MovieClip,level:int=2):void{

			//取素材可视对象的当前桢上的第一个元素（有且仅有一个元素）	
			
			
			if(level==1){
				
				childNoCache =	mc
					
			}else if(level==2){
			
				childNoCache = mc.removeChild(mc.getChildAt(0)) as MovieClip
					
			}
			
			//地图编辑器物品按放大10倍设置,需要用对象的距阵来判断缩放
			childNoCache.scaleX=_vo.scale/10
			childNoCache.scaleY=_vo.scale/10
				
			//+++++++++++++++++++不需要优化的+++++++++++++++++++++++++++++++++
			if(!isCacheBitmap){
				
				addChild(childNoCache)

				return;
				
			}
			
			//++++++++++++++++++++需要优化+++++++++++++++++++++++++++++++++++++
			child=new BitmapMovieClip(childNoCache)
				
			child.transform=childNoCache.transform
				
			addChild(child)
			
			//motionEffect()
		
		}

		/**
		 * 响应addLoader方法队列加载调用  
		 * @param e
		 * 
		 */		
		protected function addLoaderBySWF(e:ILoader):void{
			
			var queue:AbcLoaderQueue=LoaderManager.instance.getLoaderQueue(10)
			
			queue.removeLoaded(e)
			
			queue.extendsLoad()
			
			LoaderManager.instance.addLoadedGroup(e)
			
			//url_Domain_Arr.shift()
			
			//if(url_Domain_Arr.length==0){				

			addLoaderByNoSWF(linkage)
				
			//}
			
		}
		
		
		
		/**
		 * 指示对象是否完成加载状态，一般用于外部判断情况 
		 * @param linkage
		 * 
		 */		
		protected var  _isLoaded:Boolean=false;
		
		public function set isLoaded(value:Boolean):void{
			_isLoaded=value;
			
		}
		
		public function get isLoaded():Boolean{
			return 	_isLoaded
		}
		
		
		//------------显示效果优化
		private function motionEffect():void{
			
			if(_vo.type==ItemType.other_beauty){
		
		    child.scaleX= child.scaleY=.1
		
			TweenLite.to(child,1,{scaleX:1,scaleY:1,ease:Cubic.easeOut,onComplete:function():void{TweenLite.killTweensOf(child);}})
		
			}
		}
		
		/**
		 *释放位图缓存对象 
		 * 
		 */		
		public function dispose():void{
		
		   if(child) child.dispose()
		
		
		}
		
		
	}
}